package edu.ucsd.ccdb.tagPopup.dataType;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.ucsd.ccdb.tagPopup.othologyService.NifServiceWrapper;
import edu.ucsd.ccdb.tagPopup.util.BareBonesBrowserLauncher;
import edu.ucsd.irods.IRODSFileString;
import edu.ucsd.irods.SQLDBQuery;

public class OthoTagElementDisplay extends TagElementDisplay {

	/* DATA FROM SUPER
	protected JButton deleleButton;
	
	// identifiers 
	protected long MPID; 
	protected IRODSFileString file;
	protected JPanel source;
	protected String username;
	*/

	private static final long serialVersionUID = 1L;
	
	private String screeName;
	private NIFOnthologyType data;

	public OthoTagElementDisplay( String username,  JPanel source, Integer MPID, IRODSFileString file, String screenName, NIFOnthologyType data  ) {
		super( username, source, MPID, file);

		this.screeName = screenName;
		this.data = data;
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		JLabel label = new JLabel(  data.getLabel() );
		label.setForeground(Color.BLUE);
		
		label.addMouseListener(new OpenBrowser() );
		
		add (deleleButton);
		add (label);
		
		setAlignmentX(LEFT_ALIGNMENT);
	}

	@Override
	public void deleteData() {
		try {
			SQLDBQuery.deleteTagOthologyMPFile (MPID,  screeName, file.getAbsolutePath(), data);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	private class OpenBrowser implements MouseListener  {

		public void mouseClicked(MouseEvent e) {
			System.out.println ("data label, name: " + data.getLabel() + ", " + data.getName() );
			
			// Parse this data to get the neurolex url			
			BareBonesBrowserLauncher.openURL(  getURL( data.getUrl() ) );
		}

		public void mouseEntered(MouseEvent e) {			
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mousePressed(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}
		
		private String getURL ( String data ) {
			
			int startIndex = data.indexOf('#'); 
			if (startIndex != -1 ) {
				try {
					String url = NifServiceWrapper.getNeurolexURL(data.substring(startIndex + 1 ));
					return  url.equalsIgnoreCase("") ? data : url ;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			return data;
		}
		
	}

}
